package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class IconPanel extends JPanel{

	/**
	 * Class: IconPanel
	 * Description: icon + label
	 * @category GUI
	 * @author Mr3chi
	 * @since 04/05/2013
	 * modified: 04/11/2013
	 */
	private static final int BASE_SIZE = 72;
	private JLabel icon;
	private JEditorPane iconName;
	private CustomJLabel iconNameShow;
	private ImageIcon saved;
	private int size;
	
	private String name;
	private boolean isMoveon;
	private static boolean isClicked;
	private boolean isFocused;
	JPanel bot;
	private static final long serialVersionUID = 954312498260462156L;
	
	public IconPanel(ImageIcon icon, String name, int size) {
		setOpaque(false);
		this.setBackground(new Color(0,0,0,0));
		this.name = name;
		this.size = size;
		
		if (icon!= null){
			this.saved = icon;
			this.icon = new JLabel();
			Image image = this.saved.getImage().getScaledInstance(this.getIconSize(), 
					this.getIconSize(), Image.SCALE_SMOOTH);
			this.icon.setIcon(new ImageIcon(image));
		}else{
			this.icon = new JLabel();
		}
		this.iconName = new JEditorPane();
		this.iconName.setText(name);
		iconName.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
		this.iconName.setBorder(BorderFactory.createLineBorder(Color.GRAY));
        
		this.iconNameShow =new CustomJLabel();
		String tempName = "";
		if(this.name.length() >11){
			tempName = name.substring(0, 9) + "...";
		}else{
			tempName = name;
		}
		this.iconNameShow.setText(tempName);
		
		int realWidth = getRealWidth();
		
		this.setPreferredSize(new Dimension(realWidth, getRealHeight()));
		this.iconName.setPreferredSize(new Dimension(realWidth-6,realWidth/2));
		this.setLayout(new BorderLayout());
		
		JPanel top = new JPanel(new FlowLayout(FlowLayout.CENTER));
		top.add(this.icon);
		top.setOpaque(false);
		
		bot = new JPanel(new FlowLayout(FlowLayout.CENTER));
		bot.add(this.iconNameShow);
		bot.setOpaque(false);
		add(top,BorderLayout.NORTH);
		add(bot,BorderLayout.CENTER);
		MyMouseListener listener = new MyMouseListener();
		addMouseListener(listener);
		iconName.addMouseListener(listener);
		iconNameShow.addMouseListener(listener);
	}
	
	public boolean isFocused() {
		return isFocused;
	}

	public void setFocused(boolean isFocused) {
		this.isFocused = isFocused;
	}
	
	public int getIconSize() {
		return size;
	}
	
	public int getRealWidth(){
		int realWidth;
		if(size<=BASE_SIZE){
			realWidth = BASE_SIZE;
		}else{
			realWidth = size;
		}
		return realWidth;
	}
	
	public int getRealHeight(){
		return getRealWidth()+15;
	}

	public void setIconSize(int size) {
		this.size = size;
		Image image = this.saved.getImage().getScaledInstance(this.size, this.size, Image.SCALE_SMOOTH);
		this.icon.setIcon(new ImageIcon(image));
		
		int realWidth = getRealWidth();
		
		this.setPreferredSize(new Dimension(realWidth, getRealHeight()));
		this.iconName.setPreferredSize(new Dimension(realWidth,realWidth/2));
		this.setLayout(new BorderLayout());
	}

	@Override
	public String toString() {
		return this.iconName.toString();
	}

	@Override
	protected void paintComponent(Graphics g) {
		if(isMoveon && (isClicked == false)){
			g.clearRect(0, 0, this.getWidth(), this.getHeight());
			g.setColor(new Color(0, 0, 0, 160));
	        g.fillRect(0, 0, this.getWidth(), this.getHeight());
		}else{
			this.setBackground(new Color(0,0,0,0));
		} 
		if(isClicked){
			this.setBackground(new Color(0,0,0,0));
		}else{
			this.setBackground(new Color(0,0,0,0));
		}
		
	}
    
	//nested class
	class MyMouseListener extends MouseAdapter{

		@Override
		public void mouseClicked(MouseEvent e) {
			
			if(!isFocused){
				isFocused = true;
				System.out.println("fovyus");
				return;
			}
			
			if(e.getSource().equals(iconNameShow)){
				if(isFocused){
					System.out.println("asd");
					bot.remove(iconNameShow);
					iconName.setText(name);
					bot.add(iconName);
					bot.revalidate();
					bot.repaint();
				}
			}
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			isMoveon = true;
			repaint();
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			isMoveon = false;
			repaint();
		}

		@Override
		public void mousePressed(MouseEvent e) {
			isClicked = true;
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			isClicked = false;
		}
		
	}
}



